using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]
public class WeaponShootEffect : MonoBehaviour
{

    private ParticleSystem particleSystem;
    private void Awake()
    {
        particleSystem = GetComponent<ParticleSystem>();
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    public void SetShootEffect(WeaponShootEffectSO weaponShootEffectSO, float aimAngle)
    {
        SetEffectColorGradient(weaponShootEffectSO.colorGradient);
        SetEffectParticleSystemValues(weaponShootEffectSO.duration, weaponShootEffectSO.startParticleSize, weaponShootEffectSO.startParticleSpeed,
            weaponShootEffectSO.startLifeTime, weaponShootEffectSO.effectGravity, weaponShootEffectSO.maxParticleNumber);
        SetShootEffectParticleEmission(weaponShootEffectSO.emissionRate, weaponShootEffectSO.burstParticleNumber);
        SetShootEffectParticleSprite(weaponShootEffectSO.sprite);
        SetEmmitterRotation(aimAngle);
        SetShootEffectVelocityOverLifeTime(weaponShootEffectSO.velocityOverLifetimeMin, weaponShootEffectSO.velocityOverLifetimeMax);
    }

    private void SetShootEffectVelocityOverLifeTime(Vector3 velocityOverLifetimeMin, Vector3 velocityOverLifetimeMax)
    {
        ParticleSystem.VelocityOverLifetimeModule velocityOverLifetime = particleSystem.velocityOverLifetime;

        ParticleSystem.MinMaxCurve minMaxCurveX = new ParticleSystem.MinMaxCurve();
        minMaxCurveX.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveX.constantMin = velocityOverLifetimeMin.x;
        minMaxCurveX.constantMax = velocityOverLifetimeMax.x;
        velocityOverLifetime.x = minMaxCurveX;

        ParticleSystem.MinMaxCurve minMaxCurveY = new ParticleSystem.MinMaxCurve();
        minMaxCurveY.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveY.constantMin = velocityOverLifetimeMin.y;
        minMaxCurveY.constantMax = velocityOverLifetimeMax.y;
        velocityOverLifetime.y = minMaxCurveY;

        ParticleSystem.MinMaxCurve minMaxCurveZ = new ParticleSystem.MinMaxCurve();
        minMaxCurveZ.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveZ.constantMin = velocityOverLifetimeMin.z;
        minMaxCurveZ.constantMax = velocityOverLifetimeMax.z;
        velocityOverLifetime.z = minMaxCurveZ;
    }

    private void SetEmmitterRotation(float aimAngle)
    {
        transform.eulerAngles = new Vector3(0f, 0f, aimAngle);
    }

    private void SetShootEffectParticleSprite(Sprite sprite)
    {
        ParticleSystem.TextureSheetAnimationModule textureSheetAnimation = particleSystem.textureSheetAnimation;
        textureSheetAnimation.SetSprite(0, sprite);
    }

    private void SetShootEffectParticleEmission(int emissionRate, int burstParticleNumber)
    {
        ParticleSystem.EmissionModule emission = particleSystem.emission;
        ParticleSystem.Burst burst = new ParticleSystem.Burst(0f, burstParticleNumber);
        emission.SetBurst(0, burst);

        emission.rateOverTime = emissionRate;
    }

    private void SetEffectParticleSystemValues(float duration, float startParticleSize, float startParticleSpeed, float startLifeTime, float effectGravity, int maxParticleNumber)
    {
        ParticleSystem.MainModule main = particleSystem.main;
        main.duration = duration;
        main.startSize = startParticleSize;
        main.startSpeed = startParticleSpeed;
        main.startLifetime = startLifeTime;
        main.gravityModifier = effectGravity;
        main.maxParticles = maxParticleNumber;
    }

    private void SetEffectColorGradient(Gradient colorGradient)
    {
        ParticleSystem.ColorOverLifetimeModule colorOverLifetime = particleSystem.colorOverLifetime;
        colorOverLifetime.color = colorGradient;
    }
}